﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAGameEngine.GUI
{
    public abstract class GUIObject
    {
        #region local fields

        /// <summary>
        /// Reference to the GameEngine this object belongs to
        /// </summary>
        private static Game _game;

        /// <summary>
        /// Reference to the GUIManager this object belongs to
        /// </summary>
        private readonly IManagerGUI _guiManager;

        /// <summary>
        /// <para>The value of the unique integer identifier for this object.</para>
        /// <para>This value is set whenever this object is created.</para>
        /// <para>You cannot (and should not need to) change this value.</para>
        /// </summary>
        public readonly int _objectId;

        /// <summary>
        /// The bounding box for mouseover detection
        /// </summary>
        public Rectangle _boundingBox;

        /// <summary>
        /// <para>The origin of the object</para>
        /// <para>Default is 0,0 (top left corner)</para>
        /// </summary>
        public Vector2 _origin;

        /// <summary>
        /// <para>Position of the object on screen</para>
        /// <para>Default is 0,0 (top left corner)</para>
        /// </summary>
        public Vector2 _position;

        /// <summary>
        /// <para>The rotation value of the object, in degrees, to rotate around the origin</para>
        /// <para>Default is 0</para>
        /// </summary>
        public float _rotation;

        /// <summary>
        /// <para>Scale the object size</para>
        /// <para>Can be scaled in the X and Y directions</para>
        /// <para>This scales both the image and the bounding box</para>
        /// <para>Changing the x value to 2.5 will make it scale in the by 2.5x its size</para>
        /// <para>Default is 1</para>
        /// </summary>
        public Vector2 _scale;

        /// <summary>
        /// <para>The sprite effects to use when rendering</para>
        /// <para>FlipHorizontal, FlipVertical, None</para>
        /// <para>Default is None</para>
        /// </summary>
        public SpriteEffects _spriteEffects;

        /// <summary>
        /// The text to draw on this object
        /// </summary>
        public string _text;

        /// <summary>
        /// <para>The image graphic representing this object</para>
        /// </summary>
        public Texture2D _texture;

        /// <summary>
        /// <para>The image offset from the object origin</para>
        /// </para>Default is 0</para>
        /// </summary>
        public Vector2 _textureOffset;

        /// <summary>
        /// <para>Color tint applied on top of the image</para>
        /// <para>Default is white</para>
        /// </summary>
        public Color _textureTint;

        /// <summary>
        /// <para>The Z depth of the object</para>
        /// <para>This determines which objects are drawn on top of others</para>
        /// <para>Value must be between 0 and 1</para>
        /// <para>Default is 0</para>
        /// </summary>
        public float _zDepth;

        #endregion

        #region properties

        /// <summary>
        /// Returns a reference to the GUIManager
        /// </summary>
        public IManagerGUI _GUIManager
        {
            get { return _guiManager; }
        }

        /// <summary>
        /// Returns a reference to the GameEngine
        /// </summary>
        public static Game Game
        {
            get{ return _game;}
            set
            {
                if (_game != null)
                    _game = value;
            }
        }

        #endregion

        #region constructor

        /// <summary>
        /// Create a new GUIObject
        /// </summary>
        /// <param name="guiManager">pass in a reference to the GUIManager</param>
        public GUIObject(IManagerGUI guiManager)
        {
            
            _guiManager = guiManager;
            _origin = new Vector2();
            _position = new Vector2();
            _rotation = 0;
            _scale = new Vector2(1, 1);
            _spriteEffects = SpriteEffects.None;
            _textureOffset = new Vector2();
            _textureTint = Color.White;
            _zDepth = 0;
        }

        #endregion

        #region local methods

        /// <summary>
        /// Initialize the GUIObject
        /// </summary>
        public virtual void Initialize()
        { }

        /// <summary>
        /// Cleanup the GUIObject
        /// </summary>
        public virtual void Cleanup()
        { }

        /// <summary>
        /// Update the GUIObject
        /// </summary>
        /// <param name="gameTime">snapshot of the game timing values</param>
        public virtual void Update(GameTime gameTime)
        { }

        /// <summary>
        /// Draw the GUIObject
        /// </summary>
        /// <param name="gameTime">snapshot of the game timing values</param>
        public virtual void Draw(GameTime gameTime)
        {
            if (_texture != null)
                GameEngine.SpriteBatch.Draw(
                    _texture, _position, null, _textureTint,
                    _rotation, _origin, _scale, _spriteEffects, _zDepth);
        }

        #endregion
    }
}